package base

/**
 *
 * The Status class provided standard http response code and status
 *
 * @since 1.0
 */
enum class ApiStandard(
    val code: String,
    var subCode: String,
    var message: String?
) {

    /**
     * Process Success
     */
    OK("OK", "", "Success"),

    /**
     * Client Not Found
     */
    NOT_FOUND("ClientError", "NotFound", "找不到请求地址"),

    /**
     * Client Param Error Or Method Error
     */
    BAD_REQUEST("ClientError", "BadRequest", "客户端请求的语法错误，服务器无法理解"),

    /**
     * Forbidden
     */
    FORBIDDEN("ClientError", "Forbidden", "服务器理解请求客户端的请求，但是拒绝执行此请求"),

    /**
     * Invalid Request
     */
    INVALID_REQUEST(" ClientError", "InvalidRequest", "请求无效，请刷新页面重试"),

    /**
     * Unsupported Media Type
     * 415
     */
    UNSUPPORTED_MEDIA_TYPE("ClientError", "UnsupportedMediaType", "服务器无法处理请求附带的媒体格式"),

    /**
     * Unauthorized
     */
    UNAUTHORIZED("UserError", "Unauthorized", "请求要求用户的身份认证"),

    /**
     * Invalid Parameter
     */
    INVALID_PARAMETER("UserError", "InvalidParameter", "参数无效"),

    /**
     * Server Error
     */
    INTERNAL_SERVER_ERROR("InternalError", "InternalError", "服务器内部错误"),

    /**
     * Third Service Error
     */
    THIRD_SERVICE_ERROR("ThirdServiceError", "ThirdServiceError", "第三方服务错误");
}


/**
 * The Result class for http response entity payload
 *
 * @since 1.0
 */
data class ApiResponse<T>(
    var code: String = ApiStandard.OK.code,
    var message: String? = "",
    var requestId: String? = "",
    var responseEntity: T? = null
)

/**
 *@since 1.0
 */
fun <T> ok(): ApiResponse<T> = ApiResponse(ApiStandard.OK.code, "", "", null)

/**
 *@since 1.0
 */
fun <T> ok(responseEntity: T?): ApiResponse<T> = ApiResponse(responseEntity = responseEntity)

/**
 *@since 1.0
 */
fun <T> error(apiStandard: ApiStandard): ApiResponse<T> =
    ApiResponse(code = "${apiStandard.code}.${apiStandard.subCode}", message = apiStandard.message)

/**
 *@since 1.0
 */
fun <T> error(apiStandard: ApiStandard, message: String): ApiResponse<T> =
    ApiResponse(code = "${apiStandard.code}.${apiStandard.subCode}", message = message)
